// Resolves collisions between d and all other circles.
// Source: http://bl.ocks.org/1748247
function collide(alpha) {
 var quadtree = d3.geom.quadtree(nodes);
 return function(d) {
   var r = d.radius + radius.domain()[1] + padding,
       nx1 = d.x - r,
       nx2 = d.x + r,
       ny1 = d.y - r,
       ny2 = d.y + r;
   quadtree.visit(function(quad, x1, y1, x2, y2) {
     if (quad.point && (quad.point !== d)) {
       var x = d.x - quad.point.x,
           y = d.y - quad.point.y,
           l = Math.sqrt(x * x + y * y),
           r = d.radius + quad.point.radius + (d.color !== quad.point.color) * padding;
       if (l < r) {
         l = (l - r) / l * alpha;
         d.x -= x *= l;
         d.y -= y *= l;
         quad.point.x += x;
         quad.point.y += y;
       }
     }
     return x1 > nx2
         || x2 < nx1
         || y1 > ny2
         || y2 < ny1;
   });
 };
}

// Loads GET parameters.
function loadGetParams(getParams) {
  var query = window.location.search.substring(1);
  var parms = query.split('&');
  for ( var i = 0; i < parms.length; i++) {
    var pos = parms[i].indexOf('=');
    if (pos > 0) {
      var key = parms[i].substring(0, pos);
      var val = parms[i].substring(pos + 1);
      getParams[key] = decodeURIComponent(val);
    }
  }
  return getParams;
}

d3.selection.prototype.moveToFront = function() {
    return this.each(function() {
      this.parentNode.appendChild(this);
    });
  };

d3.selection.prototype.moveToBack = function() {
    return this.each(function() {
      this.parentNode.insertBefore(this, this.parentNode.firstChild);
    });
  };
